<?php

/**
 * @author liyi
 * @copyright 2018.12.26
 */

namespace Plangame\Controller;
use Common\Controller\ApiController;
use Think\Model;
use Think\Log;

class LoginController extends ApiController {
    
    const GAME_TYPE = 'Planegame';  // 游戏类型

    const DB_USER_NAME = 'plane_game.users'; // 游戏数据表

    const PLANE_SESSION = 'plane:';  //用户session_id

    const PLANE_AUTH_USER = "plane:auth_user";  //授权用户

    const PLANE_TOURIST = "plane:tourist"; //游客用户

    const USER_SHARE = 'plane:user:%s:type:%s';  // 点击分享按钮的次数

    // 登陆授权接口
    public function login()
    {
        $gameType = self::GAME_TYPE;
        $nickname = I('nickname','','string');
        $avatar = I('avatar','','string');
        $id = I('id','','int');
        $openid = I('openid','','string');

        $userInfo['openid'] = $openid;
        $userInfo['nickname'] = $nickname;
        $userInfo['avatar'] = $avatar;
        $userInfo['id'] = $id;

        $ret = D('users')->createUser($userInfo); // 创建用户
        $ret['serverTime'] = time();
 
        $this->countLog('planes',self::PLANE_AUTH_USER);  // 授权用户

        $this->response(0,'ok',$ret);
    }

    //未授权登陆接口
    public function loginNotAuth() {

        $id = I('id','','string');
        $code = I('code','','string');
        if(!$code) {
            $this->response(1,'code不能为空');
        }

        $gameType = self::GAME_TYPE;
        $info = $this->wxAuthor($gameType,$code);
        $data['openid'] = $info['openid'];

        if(!empty($info['openid'])) {
            $content['openid'] = $info['openid'];
        }else {
            $content['id'] = $id;
        }

        $users = M('users')->where($content)->find();

        if(empty($users)) {
            $data['create_time'] = date("Y-m-d H:i:s");
            $data['types'] = 1;//新用户
            $data['weapons'] = json_encode(array(1=>array('skin'=>1,'attack_lv'=>1,'speed_lv'=>1,'blood_lv'=>1,'status'=>1)));// 新用户初始武器
            $ret = M('users')->add($data);
            if(empty($ret)) {
                $this->response(1,'操作失败');
            }
            $map['id'] = $ret;
            $userInfo = M('users')->where($map)->find();
        }else{
            $data2 = null;
            if($users['types'] != 4){
                if($users['status']!=2){
                    //未授权第二次登录
                    if($users['types']!=2){
                        $data2['types'] = 2;
                    }
                }else{
                    // 授权登录
                    if(!empty($users['nickname']) && !empty($users['avatar']) && $users['types']=3){
                        $data2['types'] = 4;
                    }else{
                        $data2['types'] = 3;
                    }
                }

                if($data2){
                    $ret = M('users')->where($content)->save($data2);
                    if(empty($ret)) {
                        $this->response(1,'操作失败');
                    }
                }

            }

            $userInfo = M('users')->where($content)->find();
        }

        $userInfo['session_key'] = $info['session_key'];
        $this->countLog('planes',self::PLANE_TOURIST);  // 记录未授权用户及即游客
        $userInfo['session_id'] = $this->getSession('plane:uid',$userInfo['id']);  // 获取sessionID
        $userInfo['weapons'] = json_decode($userInfo['weapons'],true);
        $userInfo['serverTime'] = time();

        $redis = REDIS('activity');
        $userKey = sprintf(self::USER_SHARE,$userInfo['id'],3);  //用户主键分享到群

        $status = $redis->exists($userKey);
        if(!empty($status)) {
            $shareNum = $redis->get($userKey);
        }

        $userInfo['shareNum'] = !empty($status) ? $shareNum : 0;
        $this->response(0,'ok',$userInfo);

    }

    // 排行榜
    /*
     SELECT `id`,`nickname`,`avatar`,`score`,`grade` FROM `c_users` WHERE `status` = 2 AND `score` <> 0 AND `nickname`NOT IN ('undefined','') AND `avatar` NOT IN ('undefined','') ORDER BY score desc,id asc LIMIT 9
    */
}